package com.example.myapplication;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;

public class Show_Chart extends Dialog {
    private Button button_commit;
    private Button button_quit;
    private EditText edit_query;
    private TextView text_chart_title;

    //标记按钮编号
    int flag;

    private BarChart barChart;
    private YAxis leftAxis;             //左侧Y轴
    private YAxis rightAxis;            //右侧Y轴
    private XAxis xAxis;                //X轴
    private Legend legend;              //图例

    public ExecutorService exec = null;
    private DBUtils mysql_db = null;

    public Show_Chart(@NonNull Context context,ExecutorService e,DBUtils m,int index) {
        super(context);
        exec = e;
        mysql_db = m;
        flag = index;
    }

    private void initBarDataSet(BarDataSet barDataSet, int color) {
        barDataSet.setColor(color);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_chart);

        button_commit = findViewById(R.id.button_commit);
        button_quit = findViewById(R.id.button_quit);
        edit_query = findViewById(R.id.edit_query);
        barChart = findViewById(R.id.chart);
        text_chart_title = findViewById(R.id.text_chart_title);
        if(flag==1){
            text_chart_title.setText("按借阅人查询图形显示");
        }else if(flag==2){
            text_chart_title.setText("按书号查询图形显示");
        }

        ///////////////////////////////////////////////
        ///////////////////////////////////////////////
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.GRAY);
        //显示图表网格
        barChart.setDrawGridBackground(true);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(true);
        barChart.setScaleYEnabled(false);
        //设置动画效果
//        barChart.animateY(1000,);
//        barChart.animateX(1000,);
        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
//        barChart.setVisibleXRange(0,10);
        final String labelName[] = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
        // 设置x轴显示的值的格式
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if ((int) value < labelName.length) {
                    return labelName[(int) value];
                } else {
                    return "";
                }
            }
        });
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //保证Y轴从0开始，不然会上移一点
        xAxis.setAxisMinimum(0f);
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);
        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(15f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
        //不显示右下角描述内容
        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);
        //不显示X轴 Y轴线条
        xAxis.setDrawAxisLine(false);
        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);
        leftAxis.setEnabled(false);
        ArrayList<BarEntry> entries_borrow = new ArrayList<>();
        ArrayList<BarEntry> entries_return = new ArrayList<>();
        int[] arr_borrow = new int[12];
        int[] arr_return = new int[12];
        for(int a=0;a<12;++a){
            arr_borrow[a]=0;
            arr_return[a]=0;
        }
        ///////////////////////////////////////////////
        ///////////////////////////////////////////////
        // 确认按钮监听
        button_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String query = edit_query.getText().toString();
                exec.execute(new Runnable() {
                    @Override
                    public void run() {
                        mysql_db.ChartQuery(query,arr_borrow,arr_return,flag);
                    }
                });
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 12; i++) {
                    entries_borrow.add(new BarEntry(i,arr_borrow[i]));
                    entries_return.add(new BarEntry(i,arr_return[i]));
                }
                // 每一个BarDataSet代表一类柱状图
                BarDataSet barDataSet_borrow = new BarDataSet(entries_borrow, "借阅");
                BarDataSet barDataSet_return = new BarDataSet(entries_return, "归还");
                initBarDataSet(barDataSet_borrow, R.color.red);
                initBarDataSet(barDataSet_return, R.color.green);
                BarData data = new BarData(barDataSet_borrow,barDataSet_return);
                //组内柱状图之间的空间
                float barspace=0.5f;
                //每组之间的空间
                float groupspcae=0.3f;
                //柱状图的宽度
                float barWidth=(1-0.3f-0.5f*2)/2;
                //设置柱状图的宽度
                data.setBarWidth(barWidth);
                //(起始点，组间隔，组内间隔)
                data.groupBars(0f,groupspcae,barspace);
                barChart.setData(data);
                barChart.invalidate();
            }
        });
        // 退出按钮监听
        button_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
